<template>
  <div class="addobject">
    <div class="outmain ba_f heighthv pad_20 bor_rad5">
      <div class="shuju_title mar_b20">
        <div class="shuju_title_text">
          <span>商品设置</span>
        </div>
      </div>
      <div class="mar_t20 flex-bet flex-y-top">
        <el-form ref="form" :model="form" label-width="180px">
          <el-form-item label="排序" :required="true">
            <el-input v-model="form.sort" placeholder="请输入排序"></el-input>
          </el-form-item>
          <el-form-item label="商品类型" :required="true">
            <el-radio v-model="form.type" label="1">实物</el-radio>
            <el-radio v-model="form.type" label="2">虚拟</el-radio>
          </el-form-item>
          <el-form-item label="商品分类" :required="true" prop="type">
            <el-select v-model="form.typeId" placeholder="请选择">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品名称" :required="true" prop="name">
            <el-input v-model="form.name" placeholder="请输入商品名称"></el-input>
          </el-form-item>
          <el-form-item label="商品图片" :required="true" prop="icon">
            <div class="uploadSource bor_de rel flex" v-if="form.icon">
              <label class="statusLabel curs" @mouseenter="enter(1)" @mouseleave="leave(1)">
                <i class="el-icon-check" ref="imgicon1"></i>
                <i class="el-icon-close" @click="deleteMultiImage(form.icon,1)"></i>
              </label>
              <img :src="form.icon" @click="test(1)" class="wid" />
            </div>
            <div class="uploadSource flex-center curs" v-else @click="test(1)">
              <i class="el-icon-plus fon_40 color_d9"></i>
            </div>
            <div class="yb_margin">建议尺寸：720*180</div>
          </el-form-item>
          <el-form-item label="市场价" :required="true" v-show="form.type=='1'">
            <div class="t_l numberInput">
              <el-input-number v-model="form.price" controls-position="right" :min="0.01" :max="99999999" class></el-input-number>
              <span class="mar_l10">元</span>
            </div>
          </el-form-item>
          <el-form-item label="红包金额" :required="true" v-show="form.type=='2'">
            <div class="t_l numberInput">
              <el-input-number v-model="form.price" controls-position="right" :min="0.01" :max="99999999" class></el-input-number>
              <span class="mar_l10">元</span>
            </div>
          </el-form-item>
          <el-form-item label="商品库存" :required="true">
            <div class="t_l numberInput">
              <el-input-number v-model="form.stock" controls-position="right" :min="0" :max="99999999" class></el-input-number>
              <span class="mar_l10">件</span>
            </div>
          </el-form-item>
          <el-form-item label="热门商品">
            <el-tooltip :content="form.isHot==1?'是':'否'" placement="top">
              <el-switch v-model="form.isHot" active-value="1" inactive-value="2"></el-switch>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="商品状态">
            <el-tooltip :content="form.display==1?'上架':'下架'" placement="top">
              <el-switch v-model="form.display" active-value="1" inactive-value="2"></el-switch>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="商品详情">
            <baidu-ueditor :content="form.notice" @ueditor="getUeditor"></baidu-ueditor>
          </el-form-item>
          <div class="shuju_title mar_b20">
            <div class="shuju_title_text">
              <span>兑换设置</span>
            </div>
          </div>
          <el-form-item label="兑换规则" :required="true">
            <div class="t_l numberInput flex">
              <div class="mar_lr10">积分</div>
              <el-input-number v-model="form.score" controls-position="right" :min="1" :max="99999999" class></el-input-number>
              <span class="mar_lr10">积分</span>
              <div class="mar_lr10">加现金</div>
              <el-input-number v-model="form.money" controls-position="right" :min="0.00" :max="99999999" class></el-input-number>
              <span class="mar_lr10">元</span>
            </div>
          </el-form-item>
          <el-form-item label="兑换限制" :required="true">
            <div class="time_item">
              <el-radio v-model="form.isLimit" label="2">不限制</el-radio>
              <el-radio v-model="form.isLimit" label="1">限制</el-radio>
              <span v-show="form.isLimit=='1'">
                <el-input-number v-model="form.num" controls-position="right" :min="1" :max="1000000"></el-input-number>
                <span class="mar_l10">次</span>
              </span>
              <div class="yb_margin lh16">若开启了兑换限制，则顾客在本平台总共可兑换几次</div>
            </div>
          </el-form-item>
          <el-form-item label="配送支持" :required="true">
            <el-checkbox-group v-model="form.deliveryMode">
              <el-checkbox label="1" value="1">快递配送</el-checkbox>
              <el-checkbox label="2" value="2">到店自提</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="配送费" v-show="form.deliveryMode.indexOf('1')>-1">
            <div class="time_item">
              <el-input-number v-model="form.deliveryMoney" controls-position="right" :min="0" :max="99999999" class></el-input-number>
              <span class="mar_lr10">元</span>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="mar_t20 ba_f pad_20 t_c">
      <el-button type="primary" @click="submitForm">立即创建</el-button>
    </div>
    <attachment-img ref="img1" type="img" @attachment="imgadd1"></attachment-img>
  </div>
</template>
<script>
import { postsaveGoods, getSaveGoods } from '@/api/plug';
import { changeDate } from '@/utils/index';
import attachmentImg from '@/plugins/attachment-img';
import baiduUeditor from '@/plugins/baidu-ueditor';

export default {
  created() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
      this.edit();
    }
    this.typeList();
  },
  components: { attachmentImg, baiduUeditor },
  data() {
    return {
      form: {
        sort: '1',
        type: '1',
        typeId: '',
        name: '',
        icon: '',
        price: '',
        stock: '',
        isHot: '1',
        display: '1',
        notice: '',
        score: '',
        money: '',
        isLimit: '2',
        num: '',
        deliveryMode: ['1'],
        deliveryMoney: ''
      },
      options: [],
      value: '',
      collapseName: ''
    };
  },
  methods: {
    async edit() {
      const { data } = await getSaveGoods({ id: this.id });
      this.form2 = this.form;
      this.form = Object.assign(this.form2, data);
    },
    async typeList() {
      const { data } = await getSaveGoods();
      this.options = data.typeList ? data.typeList : [];
    },
    enter(i = 0) {
      this.$refs['imgicon' + i].style.display = 'none';
    },
    leave(i = 0) {
      this.$refs['imgicon' + i].style.display = 'inline-block';
    },
    test(i = 0) {
      console.log(this.$refs, i);
      this.$refs['img' + i].dialogVisible = true;
    },
    imgadd1(url) {
      this.form.icon = url.str;
    },
    deleteMultiImage(item, index, type) {
      //typem没传是单图multigraph多图；index上传图片组件排序
      Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
          this.splice(index, 1);
        }
      };
      if (type == 'multigraph') {
        switch (index) {
          case 2:
            this.form2.environment.remove(item);
            break;
        }
      } else {
        switch (index) {
          case 1:
            this.form.icon = '';
            break;
        }
      }
    },
    getUeditor(content) {
      this.form.notice = content;
    },
    async submitForm() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          const { msg } = await postsaveGoods(this.form);
          this.$baseMessage(msg, 'success');
          this.$router.go(-1);
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss">
.addobject {
  .time_item .el-input {
    width: 180px;
  }
}
</style>
